<?php
/**
 * Seller.php
 *
 * @copyright  2023 opencart.cn - All Rights Reserved
 * @link       http://www.guangdawangluo.com
 * @author     TL <mengwb@opencart.cn>
 * @created    2023-04-18 19:26:22
 * @modified   2023-04-18 19:26:22
 */

namespace Beike\Seller\Models;

use Beike\Models\Country;
use Beike\Models\Zone;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use Plugin\MultiSeller\Notifications\NewOrderNotification;

class Seller extends Base
{
    use Notifiable;
    use HasFactory;

    protected $fillable = ['seller_group_id', 'email', 'telephone', 'store_name', 'logo', 'banner', 'domain', 'domain_update_at', 'company', 'type', 'id_card', 'id_number', 'description', 'settings', 'position', 'country_id', 'zone_id', 'city_id', 'bank', 'bank_account', 'bank_name', 'product_approve', 'status', 'approved'];

    protected $casts = [
        'settings' => 'json',
    ];

    protected $appends = ['logo_url', 'banner_url'];

    public function group(): BelongsTo
    {
        return $this->belongsTo(SellerGroup::class, 'seller_group_id');
    }

    public function users(): HasMany
    {
        return $this->hasMany(SellerUser::class);
    }

    public function transactions(): HasMany
    {
        return $this->hasMany(SellerTransaction::class);
    }

    public function userGroups(): HasMany
    {
        return $this->hasMany(SellerUserGroup::class);
    }

    public function country(): BelongsTo
    {
        return $this->belongsTo(Country::class);
    }

    public function zone(): BelongsTo
    {
        return $this->belongsTo(Zone::class);
    }

    public function getLogoUrlAttribute()
    {
        if ($this->logo) {
            return image_origin($this->logo);
        }
    }

    public function getBannerUrlAttribute()
    {
        if ($this->banner) {
            return image_origin($this->banner);
        }
    }


    public function notifyNewOrderAlert($order)
    {
        $useQueue = system_setting('base.use_queue', true);
        if ($useQueue) {
            $this->notify(new NewOrderNotification($this, $order));
        } else {
            $this->notifyNow(new NewOrderNotification($this, $order));
        }
    }

}
